<template>
  <div class="canvas-box" @click="refreshCode" :style="{ height: contentHeight + 'px' }">
    <canvas ref="canvasRef" class="id-canvas" :width="contentWidth" :height="contentHeight"></canvas>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from "vue";

// 使用 defineProps 定义 props，移除了 TypeScript 类型接口
const props = defineProps({
  codeLength: {
    type: Number,
    default: 4
  },
  fontSizeMin: { type: Number, default: 25 },
  fontSizeMax: { type: Number, default: 35 },
  contentWidth: { type: Number, default: 100 },
  contentHeight: { type: Number, default: 40 },
  backgroundColorMin: {
    type: Number,
    default: 200
  },
  backgroundColorMax: {
    type: Number,
    default: 220
  },
  dotColorMin: {
    type: Number,
    default: 60
  },
  dotColorMax: {
    type: Number,
    default: 120
  },
});
// 定义组件要发射的事件
const emit = defineEmits(['update:code']);

// --- 内部状态 ---
const canvasRef = ref(null);
const identifyCodes = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
const generatedCode = ref(""); // 存储内部生成的验证码

// 生成一个随机数 (移除类型注解)
const randomNum = (min, max) => {
  return Math.floor(Math.random() * (max - min) + min);
};

// 生成一个随机的颜色 (移除类型注解)
const randomColor = (min, max) => {
  let r = randomNum(min, max);
  let g = randomNum(min, max);
  let b = randomNum(min, max);
  return "rgb(" + r + "," + g + "," + b + ")";
};

const drawPic = () => {
  const canvas = canvasRef.value;
  if (!canvas) return;
  const ctx = canvas.getContext('2d');
  if (!ctx) return;
  ctx.textBaseline = "bottom";
  // 绘制背景
  ctx.fillStyle = "#e6ecfd";
  ctx.fillRect(0, 0, props.contentWidth, props.contentHeight);
  // 绘制文字
  for (let i = 0; i < generatedCode.value.length; i++) {
    drawText(ctx, generatedCode.value[i], i);
  }
  drawLine(ctx);
  drawDot(ctx);
};

// 绘制文字
const drawText = (ctx, txt, i) => {
  ctx.fillStyle = randomColor(50, 160);
  ctx.font = `${randomNum(props.fontSizeMin, props.fontSizeMax)}px SimHei`;
  const x = (i + 1) * (props.contentWidth / (generatedCode.value.length + 1));
  const y = randomNum(props.fontSizeMax, props.contentHeight - 5);
  const deg = randomNum(-30, 30);
  ctx.translate(x, y);
  ctx.rotate((deg * Math.PI) / 180);
  ctx.fillText(txt, 0, 0);
  ctx.rotate((-deg * Math.PI) / 180);
  ctx.translate(-x, -y);
};

const drawLine = (ctx) => {
  // 绘制干扰线
  for (let i = 0; i < 4; i++) {
    ctx.strokeStyle = randomColor(100, 200);
    ctx.beginPath();
    ctx.moveTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight));
    ctx.lineTo(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight));
    ctx.stroke();
  }
};

const drawDot = (ctx) => {
  // 绘制干扰点
  for (let i = 0; i < 30; i++) {
    ctx.fillStyle = randomColor(0, 255);
    ctx.beginPath();
    ctx.arc(randomNum(0, props.contentWidth), randomNum(0, props.contentHeight), 1, 0, 2 * Math.PI);
    ctx.fill();
  }
};

// 生成并刷新验证码
const refreshCode = () => {
  let newCode = "";
  for (let i = 0; i < props.codeLength; i++) {
    newCode += identifyCodes[randomNum(0, identifyCodes.length)];
  }
  generatedCode.value = newCode;
};

// 监听内部验证码的变化
watch(generatedCode, (newValue) => {
  // 1. 重新绘制画布
  drawPic();
  // 2. 将新生成的验证码通过事件发射给父组件
  emit('update:code', newValue);
});

// 组件挂载时，生成初始验证码
onMounted(() => {
  refreshCode();
});

// 使用 defineExpose 将 refreshCode 方法暴露出去，让父组件可以调用
defineExpose({
  refreshCode
});
</script>

<style scoped>
.canvas-box {
  cursor: pointer;
}
.id-canvas {
  height: 100%;
}
</style>
